package cate.game.pvp.throne.data.ko;

import cate.common.table.activity.throne.TBKoRoundRow;
import cate.common.table.activity.throne.TBKoRoundSheet;
import cate.common.table.d.GDThrone;
import cate.common.util.TimeTool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


@NoteClass("王座争霸-淘汰赛阶段时刻表")
@Slf4j
@Getter
public class TBKoTimeTable {

    @NoteField(value = "当前轮次",detail = "刚进入选拔赛是是第0轮次")
    private int round;

    @NoteField("当前阶段")
    private byte period;

    @NoteField("淘汰赛时间表")
    private List<TBKoRoundTime> roundTimes;

    @NoteField(value = "下次检测时间",detail = "就是当前阶段结束的时间")
    private long checkTime;

    public void initialize() {
        if (roundTimes == null) {
            roundTimes = new ArrayList<>();
        }
        resetCheckTime();
    }

    /**
     * 开启一期新的活动时
     * @param startTime
     */
    public void onStart(TBKoRoundSheet sheet,long startTime) {
        this.round = 0;
        this.period = 0;
        this.roundTimes.clear();
        this.checkTime = 0L;
        buildRoundTime(sheet,startTime);
        resetCheckTime();
    }

    @JsonIgnore
    public long getCheckTime() {
        // 获取下一个阶段的开始时间
        int nextRound;
        int nextPeriod;

        if (this.round == 0) {
            nextRound = 1;
            nextPeriod = GDThrone.KoPeriod.BET;
        } else {
            nextRound = this.round;
            nextPeriod = this.period + 1;
            if (nextPeriod > GDThrone.KoPeriod.REST) {
                nextRound++;
                nextPeriod = GDThrone.KoPeriod.BET;
            }
        }

        for (TBKoRoundTime time : roundTimes) {
            if (time.round == nextRound) {
                return time.getTime(nextPeriod);
            }
        }
        return 0L;
    }

    public void resetCheckTime() {
        checkTime = getCheckTime();
    }


    /**
     * 构建时间列表
     * @param startTime 淘汰赛阶段开始时间
     */
    private void buildRoundTime(TBKoRoundSheet sheet, long startTime) {
        if (startTime > 0) {
            for (TBKoRoundRow row : sheet.getList()) {
                long roundStartTime = TimeTool.getRealTimestamp(startTime, row.openDay - 1, row.timeStr);
                TBKoRoundTime roundTime = new TBKoRoundTime(row.round);
                roundTime.buildTime(roundStartTime, row);
                roundTimes.add(roundTime);
            }
        }
    }


    @JsonIgnore
    public boolean tick() {
        if (checkTime == 0L || System.currentTimeMillis() < checkTime) {
            return false;
        }
        toNextPeriod();
        resetCheckTime();
        log.info("王座争霸淘汰赛阶段变化,目前是第{}轮的 {} ,下个阶段的开始时间是{}",
                round, GDThrone.KoPeriod.des(period), TimeTool.getDF2().format(checkTime));
        return true;
    }


    /** 尝试进入下一个阶段 */
    private void toNextPeriod() {
        if (round == 0) {
            this.round = 1;
            this.period = GDThrone.KoPeriod.BET;
        } else {
            this.period++;
            if (period > GDThrone.KoPeriod.REST) {
                round++;
                period = GDThrone.KoPeriod.BET;
            }
        }
    }

    public boolean inBet() {
        return period == GDThrone.KoPeriod.BET || period == GDThrone.KoPeriod.CAL;
    }

    public void clear() {
        round = 0;
        period = 0;
        roundTimes.clear();
        checkTime = 0L;
    }

}
